/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdesk;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.ArrayList;

import java.sql.SQLException;

/**
 * View all tickets. Parameters are optional and are used for filters. Absence
 * of a parameter does not enable filter.
 *
 * TODO: fix filters
 *
 * Parameters:
 * 1) userId - show tickets created by user userId. Absence means all users.
 * 2) supportUserId - show tickets with support user supportUserId. Absence
 *    means tickets with any or none support user are displayed.
 * 3) open - show tickets with no support user. default true
 * 4) accepted - show tickets that have been accepted. default true
 * 5) closed - show tickets that have been closed. default true
 * 6) offset - Start getting tickets after this ticket. default 0
 * 7) limit - How many entries to get. Maximum is 100. Default is 100.
 *
 * @author brian
 */
public class ViewTicketsServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        ArrayList<HelpTicket> helpTickets;
        String error = new String();
        RequestDispatcher rd;

        boolean opened, closed, accepted;
        int owned, notowned;

        try {
            if (HelpdeskUtil.authenticateUser(request, response) != null)
            {
                
                //Parse the parameters
                opened = new Boolean(request.getParameter("opened")).booleanValue();
                closed = new Boolean(request.getParameter("closed")).booleanValue();
                
                try{
                    owned = new Integer(request.getParameter("owned")).intValue();
                }
                catch(NumberFormatException e)
                {
                    owned = 0;
                }
                try{
                    notowned = new Integer(request.getParameter("notowned")).intValue();
                }
                catch(NumberFormatException e)
                {
                    notowned = 0;
                }
                accepted = new Boolean(request.getParameter("notowned")).booleanValue();
                                
                try
                {
                    helpTickets = HelpTicket.getHelpTickets(100,opened,closed, owned, notowned, accepted);

                    request.setAttribute("tickets", helpTickets);
                }
                catch (SQLException ex)
                {
                    error = "SQL Exception: " + ex.getMessage();
                }

                request.setAttribute("error", error);
                rd = request.getRequestDispatcher("/WEB-INF/viewtickets.jsp");
                rd.forward(request, response);
            }


        } finally { 
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
